<?php
set_time_limit(0);

$ip = '127.0.0.1';
$port = 8999;

if(($sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0) {
    echo "socket_create() 失败的原因是:".socket_strerror($sock)."\n";
}

if(($ret = socket_bind($sock,$ip,$port)) < 0) {
    echo "socket_bind() 失败的原因是:".socket_strerror($ret)."\n";
}

if(($ret = socket_listen($sock,4)) < 0) {
    echo "socket_listen() 失败的原因是:".socket_strerror($ret)."\n";
}

do{
	$accept_resource = socket_accept($sock);
	printf("接受Socket连接".PHP_EOL);
	
	if($accept_resource !== false){
		$string = socket_read($accept_resource,1024);
		echo '服务器接收到:'.$string.PHP_EOL;
	}
	
	if($string != false){
		$return_client = '服务器返回：'.$string.PHP_EOL;
		socket_write($accept_resource,$return_client,strlen($return_client));
		echo $return_client;
	}else{
		echo 'socket_read is fail';
	}
	socket_close($accept_resource);
}while(true);
socket_close($sock);